package com.yiyuan.weixin.weixinInservice.Iml;

import com.yiyuan.weixin.model.ReceiveXmlEntity;
import com.yiyuan.weixin.utils.ReceiveXmlProcess;
import com.yiyuan.weixin.weixinInservice.WXResponseDispatcherUtil;
import com.yiyuan.weixin.weixinInservice.service.WXAccessService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by admin on 2017/3/28.
 */
public class WXAccessIml  extends AbsWXAccessService {
    @Override
    public void weiXinAccess(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        /** 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回 */
        String echostr = req.getParameter("echostr");
        if (echostr != null && echostr.length() > 1) {
            resp.getWriter().write(echostr);
            return;
        }
        String reqMessage=getStringFromWX(req);
        ReceiveXmlEntity xmlEntity = new ReceiveXmlProcess().getMsgEntity(reqMessage);
        WXResponseDispatcherUtil.messageDispatcher(resp,xmlEntity);
    }
}
